import '../style/visual.less';


import {InspectorService} from "./service/InspectorService";

import {World} from "./map-animate/map-gd";
import {StaticGroup} from "./model";
import {ModelService} from "./service/ModelService";

export default class Visual extends WynVisual {
  private inspectorService: InspectorService;
  private staticGroup: StaticGroup;
  private loadStatus=false;
  private dom:any;
  private canvas:any;

  constructor(dom: HTMLDivElement, host: VisualNS.VisualHost, options: VisualNS.IVisualUpdateOptions) {
    super(dom, host, options);
    StaticGroup.initialize();
    this.dom=dom;
    this.inspectorService = new InspectorService();
    this.staticGroup=new StaticGroup();
    this.canvas=document.createElement("canvas")
    this.canvas.id="canvas_id";
    this.dom.append(this.canvas)

    StaticGroup.host = host;
    StaticGroup.selectionManager = host.selectionService.createSelectionManager();
    StaticGroup.selectionOption = [];
    StaticGroup.format = {};
    this.loadStatus=false;
  }

  public update(options: VisualNS.IVisualUpdateOptions) {
    this.inspectorService.setInspectorAttributes(options.properties);
    new ModelService().getData(StaticGroup.host, options);

    this.initMap();
    if (options.updateType === 'viewportChange') { //窗口变化
      this.onReSize();
    } else if (options.updateType === 'dataViewChange') { //数据变化
      if (StaticGroup.app && this.loadStatus) {
        StaticGroup.app.reloadBar()
      }
      this.loadStatus=true;
    } else if (options.updateType === 'propertyChange') { //属性变化
      if (StaticGroup.app && this.loadStatus) {
        if (StaticGroup.options.base_adcode===StaticGroup.old_options.base_adcode){
          StaticGroup.app.setAttributeSetting();
          //判断是否加载数据
          if (this.isreload()){
            StaticGroup.app.reloadBar()
          }
        }
      }
    }

    //加载完成之后,将属性保存
    StaticGroup.old_options=StaticGroup.options;
  }

  /**
   * 判断什么情况下需要重新加载数据
   */
  public isreload(){
    const keys=["data_num_show","data_num"];
    let bol=false;
    keys.forEach(key=>{
      if (StaticGroup.options[key]!==StaticGroup.old_options[key]){
        bol= true;
      }
    })
    return bol;
  }

  public initMap(){
    //判断
    const isLoad=StaticGroup.adcode !== StaticGroup.before_adcode;
    if (isLoad && StaticGroup.isSuccess){
      if (StaticGroup.app){
        StaticGroup.app.clearMap();
      }
      StaticGroup.isSuccess=false;
      StaticGroup.app=new World(this.canvas);
      StaticGroup.before_adcode = StaticGroup.adcode;
    }
  }

  public onDestroy(): void {
    if (StaticGroup.app){
      StaticGroup.app.divresize();
    }
  }
  public onReSize(): void {
    if (StaticGroup.app){
      StaticGroup.app.divresize();
    }
  }
  public getInspectorHiddenState(options: VisualNS.IVisualUpdateOptions): string[] {
    return null;
  }

  public getActionBarHiddenState(options: VisualNS.IVisualUpdateOptions): string[] {
    return null;
  }

  public getColorAssignmentConfigMapping(dataViews: VisualNS.IDataView[]): VisualNS.IColorAssignmentConfigMapping {
    return null;
  }
}